package com.example.myfoodrecipe.fragments.favorite

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myfoodrecipe.data.model.Result
import com.example.myfoodrecipe.databinding.FragmentFavBinding
import com.example.myfoodrecipe.viewModel.FavViewModel

class FavFragment : Fragment() {
    private lateinit var binding: FragmentFavBinding
    private val favViewModel: FavViewModel by viewModels()
    private val favAdapter = FavAdapter()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        binding = FragmentFavBinding.inflate(inflater)
        //init fav_recyclerview
        binding.recyclerView.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        binding.recyclerView.adapter = favAdapter

        //To_DO实现右滑删除
        ItemTouchHelper(object: ItemTouchHelper.Callback(){
            override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
                TODO("Not yet implemented")
            }

            override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
                TODO("Not yet implemented")
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                TODO("Not yet implemented")
            }

        })

            //更新数据
        favViewModel.getAllFav()
        favViewModel.favorites.observe(viewLifecycleOwner){
            val recipes = mutableListOf<Result>()
            it.forEach { entity->
                recipes.add(entity.recipe)
            }
            favAdapter.setData(recipes)
        }

        return binding.root
    }

}